function setLocation(){
	var gpsCords=new Array();
	gpsCords[0]=$("#deg_out_1 .gCord").text();
	gpsCords[1]=$("#deg_out_2 .gCord").text();
	//console.log("gpsCords:"+gpsCords);
	return gpsCords;
}
$(document).ready(function(){	
	var thisHref=new Array();
	var thisTitle=new Array();
	var thisMap1=0;
	$("MAP[name='mindencache'] AREA[title]").each(function(e){		
		if(thisMap1!=0){
			thisHref[thisMap1]=$(this).attr("href");
			thisTitle[thisMap1]=$(this).attr("title");		
			$(this).removeAttr("href");
			locations[thisMap1]=new Array(thisTitle[thisMap1],'0','0','0');			
		}
		thisMap1=e+1;
	});
	//console.log(locations);
	$("MAP[name='mindencache'] AREA[title]").each(function(e){
		$(this).click(function(){
				var thisMap=e;					
				var thisCacheId=thisHref[thisMap].substring(thisHref[thisMap].search("id=")+3);	
				//console.log(thisCacheId);
				$.ajax({
						url: 'http://geocaching.hu/caches.geo',
						method: 'post',
						data: 'id='+thisCacheId,
						success: function(msg) {
						var dataCrop=msg.substr(msg.search("width=580><div>"),220);
						
						var data_1=dataCrop.substring(dataCrop.search('<span title="N '),dataCrop.search('&deg;" class="strong">N'));
						data_1=data_1.substr(15);
						var data_2=dataCrop.substring(dataCrop.search('<span title="E '),dataCrop.search('&deg;" class="strong">E'));
						data_2=data_2.substr(15);
						
						//var getLocation=setLocation();
						//console.log(getLocation[0]);	
						locations[thisMap]=new Array(thisTitle[thisMap],data_1,data_2,'0'); 
						
						zoomToMarker(locations,thisMap,thisHref);
						$("#gpsConvInput_1").attr("value",data_1);
						$("#gpsConvInput_2").attr("value",data_2);
						upd($("#gpsConvInput_1").val(),'deg_out_1');
						upd($("#gpsConvInput_2").val(),'deg_out_2');	
						$('HTML, BODY').animate({
					 		scrollTop:$("#runCords_1").offset().top 
						},  100, 							 
							function() {  } 
				 		);	 																						
					 }
				});				
		 	
			//console.log(thisHref[e]+" ||||| "+thisTitle[e]);
		});
		
	});
	$("#runCords_1").click(function(){		
		zoomToMarker(locations,0);
		$("#gpsConvInput_1").attr("value",locations[0][1]);
		$("#gpsConvInput_2").attr("value",locations[0][2]);
		//console.log(locations);
	});
	
	$("#searchCacheBtn").click(function(){		
		var thisMap=location.length+1;					
		var thisCacheCode=$("#searchCacheField").val();	
		//console.log(thisCacheId);
		if(thisCacheCode.length>=5){		
			$.ajax({
					url: 'http://geocaching.hu/caches.geo',
					method: 'post',
					data: 'any='+thisCacheCode,
					success: function(msg) {
						thisCacheCode=">"+thisCacheCode+"<";
					 	//console.log(thisCacheId);
						//console.log(thisCacheId+","+msg.search(/thisCacheId+"<"/i));
						msg=msg.toLowerCase();
						//console.log(msg.indexOf(thisCacheId));
						var startText=msg.indexOf(thisCacheCode);
						var data_3=msg.substring(startText-20,startText-13);
						data_3=data_3.replace(" ","");
						var mytool_array=data_3.split("=");
						data_3=(mytool_array[1]);						
						//console.log(data_3);
						$("#searchCacheForm").attr("name",data_3);		
						console.log("data_3:"+data_3);			
					}
					
			});	
			
			t1=setTimeout(function(){thisCacheId=$("#searchCacheForm").attr("name"); console.log("t:"+thisCacheId); ajaxCacheId(locations,thisMap,thisHref,thisCacheId)},1600);
			//console.log(thisCacheId);
			function ajaxCacheId(locations,thisMap,thisHref,thisCacheId){
			$.ajax({
					url: 'http://geocaching.hu/caches.geo',
					method: 'post',
					data: 'id='+thisCacheId,
					success: function(msg) {
					var dataCrop=msg.substr(msg.search("width=580><div>"),220);
					
					var data_1=dataCrop.substring(dataCrop.search('<span title="N '),dataCrop.search('&deg;" class="strong">N'));
					data_1=data_1.substr(15);
					var data_2=dataCrop.substring(dataCrop.search('<span title="E '),dataCrop.search('&deg;" class="strong">E'));
					data_2=data_2.substr(15);
					
					//var getLocation=setLocation();
					console.log("thisCacheId:"+thisCacheId);	
					locations[thisMap]=new Array(thisTitle[thisMap],data_1,data_2,'0'); 
					
					zoomToMarker(locations,thisMap,thisHref);
					$("#gpsConvInput_1").attr("value",data_1);
					$("#gpsConvInput_2").attr("value",data_2);
					upd($("#gpsConvInput_1").val(),'deg_out_1');
					upd($("#gpsConvInput_2").val(),'deg_out_2');	
					$('HTML, BODY').animate({
						scrollTop:$("#runCords_1").offset().top 
					},  100, 							 
						function() {  } 
					);	 																						
				 }
			});
			}
			
		}else {
			alert("A geolada neve 6 karakter hosszunak kell lennie! Az elso ketto karakter a kovetkezo: GC");
		}
	});
	
});
	var getLocation=setLocation();
	var locations=new Array(); locations[0]=new Array('<b>JollyJokers:</b> <br> Az alapt&aacute;bor! :)',getLocation[0],getLocation[1],'0');
	zoomToMarker(locations,0);
	
	/*
	var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 1],
      ['Coogee Beach', -33.923036, 151.259052, 2],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 4],
      ['Maroubra Beach', -33.950198, 151.259302, 5]
    ];
*/
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 13,
      center: new google.maps.LatLng(47.535044, 21.634054),
      mapTypeId: google.maps.MapTypeId.HYBRID
    });

    var infowindow = new google.maps.InfoWindow();
    var marker, i;
	i=0;
	$("#gpsConvInput_1").attr("value",locations[0][1]);
	$("#gpsConvInput_2").attr("value",locations[0][2]);

    //for (var i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    //}
	
	function zoomToMarker(locations,id,thisHref){  	
		var addId=id-1;
		//console.log(addId+"-"+id);
		var map = new google.maps.Map(document.getElementById('map'), {
		  zoom: 16,
		  center: new google.maps.LatLng(locations[id][1], locations[id][2]),
		  mapTypeId: google.maps.MapTypeId.HYBRID
		});
	
		var infowindow = new google.maps.InfoWindow();
		var marker, i;

		//for (var i = 0; i < locations.length; i++) {  
		  marker = new google.maps.Marker({
			position: new google.maps.LatLng(locations[id][1], locations[id][2]),
			map: map
		  });
		  marker.setDraggable (true);
		  google.maps.event.addListener(marker, "dragend", function(event) {
			var point = marker.getPosition();
			map.panTo(point);			   	
			$("#gpsConvInput_1").attr("value",point.lat());
			$("#gpsConvInput_2").attr("value",point.lng());
			upd($("#gpsConvInput_1").val(),'deg_out_1');
			upd($("#gpsConvInput_2").val(),'deg_out_2');	
			//console.log(point.lat()+","+point.lng());
			//console.log($("#gpsConvInput_1").val()+","+$("#gpsConvInput_2").val());	
		  });
		   
			   	  
		  google.maps.event.addListener(marker, 'click', (function(marker, id) {
				return function() {
				  infowindow.setContent("<b>"+locations[id][0]+"</b><br>lat="+locations[id][1]+"<br>lng="+locations[id][2]+"<br><a href='"+thisHref[id]+"'>Geol&aacute;da llinkje</a>");
				  infowindow.open(map, marker);
				}
		  })(marker, id));	
		//}
	}
	
	